package code15Lambda表达式;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author 黄衡熙
 * @version 1.0
 */
public class Test10_终止操作 {
    private static List<Integer> list = Arrays.asList(5, 1, 3, 9, 13, 16, 8, 2, 18, 7, 9);

    public static void main(String[] args) {
        //allMatchTest();
        //anyMatchTest();
       // noneMatchTest();
        //        findFirstTest();
//        findAnyTest();
//        countTest();
//        maxTest();
//        minTest();
//        reduceTest();
        collectTest();
    }

    /*
     * 检查流中所有的元素是否符合制定规则*/
    public static void allMatchTest() {
        boolean flag = list.stream()
                .allMatch(i -> i > 0);
        System.out.println("流中元素是否都大于0:" + flag);
    }

    /*
     * 检查流中是否包含指定规则的元素
     * */
    public static void anyMatchTest() {
        boolean flag = list.stream()
                .anyMatch(i -> i > 0);
        System.out.println("流中是否存在大于10的元素:" + flag);
        System.out.println("-------------------------------");
        boolean flag2 = list.stream()
                .allMatch(i -> i > 19);
        System.out.println("流中是否存在大于20的元素:" + flag2);
    }

    /*
     * 检查流中所有的元素是否都不满足制定规则
     * */
    public static void noneMatchTest() {
        boolean flag = list.stream()
                .noneMatch(i -> i > 0);
        System.out.println("流中所有的元素是否都不存在大于0:" + flag);
        System.out.println("------------------------");
        boolean flag2 = list.stream()
                .noneMatch(i -> i > 20);
        System.out.println("流中所有的元素是否都不存在大于19:" + flag2);
    }

    public static void findFirstTest() {
//        list = new ArrayList<>();
        // Optional:容器对象
        Optional<Integer> first = list.stream().findFirst();
        // 通过get方法从容器中取值
        // 如果容器中没有值,则抛出异常
//        System.out.println(first.get());
        System.out.println("容器中值是否为空:" + first.isEmpty());
        System.out.println("容器中值是否不为空:" + first.isPresent());
        Integer num = 0;
        if (first.isPresent()) {
            num = first.get();
        }
        System.out.println(num);
    }

    public static void findAnyTest() {
        Optional<Integer> any = list.stream().findAny();
        System.out.println("当前线程:" + any.get());

        new Thread() {
            @Override
            public void run() {
                // parallelStream():获取并行流
                Optional<Integer> any = list.parallelStream().findAny();
                System.out.println("另一个线程:" + any.get());
            }
        }.start();
    }

    public static void countTest() {
        long count = list.stream().count();
        System.out.println("流中元素的数量:" + count);
    }


    public static void maxTest() {
        Optional<Integer> max = list.stream().max((a, b) -> a - b);
        System.out.println("最大值:" + max.get());
        Optional<Integer> min = list.stream().max((a, b) -> b - a);
        System.out.println("最小值:" + min.get());
    }

    public static void minTest() {
        Optional<Integer> min = list.stream().min((a, b) -> a - b);
        System.out.println("最小值:" + min.get());
        Optional<Integer> max = list.stream().min((a, b) -> b - a);
        System.out.println("最大值:" + max.get());
    }

    public static void reduceTest() {
        list = Arrays.asList(1, 2, 3);
        // Optional<Integer> reduce = list.stream().reduce((a, b) -> a + b);
        // 在Integer中提供了求和的方法,可以直接使用
        Optional<Integer> reduce = list.stream().reduce(Integer::sum);
        System.out.println(reduce.get());

        // 1-100之间整数之和
        Optional<Integer> reduce2 = Stream.iterate(1, i -> i + 1)
                .limit(100)
                .reduce(Integer::sum);
        System.out.println(reduce2.get());
    }

    public static void collectTest() {
        List<Integer> list2 = list.stream()
                .collect(Collectors.toList());
        System.out.println(list2);
        Set<Integer> set = list.stream()
                .collect(Collectors.toSet());
        System.out.println(set);
    }
}
